package com.bls.executor.parser;

import com.bls.exception.MyException;
import com.bls.executor.node.sentence.simpleStmt.*;
import com.rabbitmq.client.Channel;
import org.antlr.v4.runtime.tree.ParseTree;

import java.util.*;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledFuture;

public class Visitor extends V3BaseVisitor {
    public static Map<String, Object> scriptEnvironment = new HashMap<>();
    public Future task = null;
    public Map<String, Channel> tag = new Hashtable<>();

    @Override
    public Map<String, Object> visitIntenseStmt(V3Parser.IntenseStmtContext ctx) {
        Map<String, Object> result = new HashMap<>();
        IntenseStmt intenseStmt = null;
        boolean runResult = false;
        try {
            if (ctx.runToolStmt() != null) {
                if (ctx.runToolStmt().notifyStmt() == null) {
                    intenseStmt = new RunToolStmt(ctx.runToolStmt(), scriptEnvironment);

                } else {
                    intenseStmt = new NotifyStmt(ctx.runToolStmt().notifyStmt(), scriptEnvironment);
                }
                runResult = intenseStmt.run("");
            }else if(ctx.authoriseStmt() != null){
                return visitAuthoriseStmt(ctx.authoriseStmt());
            }
        } catch (MyException e) {
            result.put("exception", e);
            result.put("runResult", runResult);
            return result;
        }
        result.put("runResult", runResult);
        return result;
    }


    @Override
    public Map<String, Object> visitListenStmt(V3Parser.ListenStmtContext ctx) {
        Map<String, Object> result = new HashMap<>();
        ListenStmt listenStmt = null;
        try {
            listenStmt = new ListenStmt(ctx, scriptEnvironment);
            tag = listenStmt.runTask();
        } catch (MyException e) {
            result.put("exception", e);
            result.put("runResult", false);
            return result;
        }
        result.put("runResult", true);
        return result;
    }

    @Override
    public Map<String, Object> visitScheduleStmt(V3Parser.ScheduleStmtContext ctx) {
        Map<String, Object> result = new HashMap<>();
        ScheduledStmt scheduledStmt = null;
        try {
            scheduledStmt = new ScheduledStmt(ctx, scriptEnvironment);
            task = scheduledStmt.runTask();
        } catch (MyException e) {
            result.put("exception", e);
            result.put("runResult", false);
            return result;
        }
        result.put("runResult", true);
        return result;
    }

    @Override
    public Map<String, Object> visitAuthoriseStmt(V3Parser.AuthoriseStmtContext ctx) {
        Map<String, Object> result = new HashMap<>();
        AuthoriseStmt authoriseStmt = null;
        boolean runResult = false;
        try {
            authoriseStmt = new AuthoriseStmt(ctx, scriptEnvironment);
            runResult = authoriseStmt.run("");
        } catch (MyException e) {
            result.put("exception", e);
            result.put("runResult", runResult);
        }
        return result;
    }

    @Override
    public Object visitSetStmt(V3Parser.SetStmtContext ctx) {
        return null;
    }

    @Override
    public  Map<String, Object> visit(ParseTree tree) {
        Map<String, Object> visit = (Map<String, Object>) super.visit(tree);
        if(task != null) visit.put("task",task);
        else if(tag.size() != 0) visit.put("tag",tag);
        return visit;
    }

}
